﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DatabaseModule;

namespace EMC
{
    public class FriendManagement
    {
        private DAO dbConn;

        private databaseConfiguration config;

        public FriendManagement(databaseConfiguration dbConfig)
        {
            this.config = dbConfig;
            dbConn = dbFactory.newDB(config.dbType, config.dbConnectionString);
            //dbConn.SetVerboseMode(true);
        }

        public List<string> ListFriends(string userid)
        {
            KeyValues keyValsMeFirst = new KeyValues();
            keyValsMeFirst.Add(config.tableFriends_UserID1, userid);
            List<Dictionary<string, string>> meFirst = dbConn.GetRows(config.tableFriends, new string[] { config.tableFriends_UserID2 }, keyValsMeFirst);

            KeyValues keyValsMeSecond = new KeyValues();
            keyValsMeSecond.Add(config.tableFriends_UserID2, userid);
            List<Dictionary<string, string>> meSecond = dbConn.GetRows(config.tableFriends, new string[] { config.tableFriends_UserID1 }, keyValsMeSecond);
            
            List<string> friends = meFirst.ConsolidateValues(config.tableFriends_UserID2);
            friends.AddRange(meSecond.ConsolidateValues(config.tableFriends_UserID1));
            friends.Sort();
            
            return friends;
        }

        private KeyValues generateFlexibleConditional(string userid1,string userid2)
        {
            KeyValues friendConditions = new KeyValues();
            friendConditions.Add(config.tableFriends_UserID1, userid1, ConditionMode.ModeAnd);
            friendConditions.Add(config.tableFriends_UserID2, userid2, ConditionMode.ModeTopLevelOr);
            friendConditions.Add(config.tableFriends_UserID1, userid2, ConditionMode.ModeAnd);
            friendConditions.Add(config.tableFriends_UserID2, userid1, ConditionMode.ModeTopLevelOr);
            return friendConditions;
        }

        public bool checkfriend(string userid1, string userid2)
        {

            if (dbConn.GetValue(config.tableFriends, config.tableFriends_UserID1, generateFlexibleConditional(userid1,userid2)) != null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public bool addfriend(string userid1, string userid2)
        {
            if (checkfriend(userid1, userid2) == false)
            {
                KeyValues keyvals = new KeyValues();
                keyvals.Add(config.tableFriends_UserID1, userid1);
                keyvals.Add(config.tableFriends_UserID2, userid2);
                dbConn.InsertRow(config.tableFriends, keyvals);
                return true;
            }

            else
            {
                return false;
            }
        }
        public bool removefriend(string userid1,string userid2) 
        {
            return dbConn.DeleteRows(config.tableFriends, generateFlexibleConditional(userid1, userid2));
        }
    }
}
